package dao;

// Generated Mar 18, 2014 10:39:39 PM by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import pojo.Mobile;

/**
 * Home object for domain model class Mobile.
 * @see pojo.Mobile
 * @author Hibernate Tools
 */
public class MobileHome {

    private final Session session;

    public MobileHome(SessionFactory sessionFactory) {
        session = sessionFactory.openSession();
    }
    
    public void createMobile(Mobile mobile) {
        session.beginTransaction();
        session.persist(mobile);
        session.getTransaction().commit();
    }
    
    public void removeMobile(Mobile mobile) {
        session.beginTransaction();
        session.delete(mobile);
        session.getTransaction().commit();
    }
    
    public Mobile getMobileById(int id) {
        return (Mobile) session.load(Mobile.class, id);
    }
    
    public List getLatestMobiles(int i) {
        return session.createCriteria(Mobile.class)
                .addOrder(Order.desc("id"))
                .setMaxResults(i)
                .list();
    }
    
    public List getLatestMobilesByCompany(String company) {
        return session.createCriteria(Mobile.class)
                .add(Restrictions.ilike("company", company))
                .addOrder(Order.desc("id")).setMaxResults(15)
                .list();
    }
    
    public List getLatestMobilesBetweenPriceRange(int min, int max) {
        return session.createCriteria(Mobile.class)
                .add(Restrictions.le("price", max))
                .add(Restrictions.ge("price", min))
                .addOrder(Order.desc("id")).setMaxResults(15)
                .list();
    }
    
    public List getLatestMobilesByName(String name) {
        return session.createCriteria(Mobile.class)
                .add(Restrictions.ilike("name", name, MatchMode.ANYWHERE))
                .addOrder(Order.desc("id")).setMaxResults(15)
                .list();
    }
    
    public void updateMobile(Mobile mobile) {
        session.beginTransaction();
        session.update(mobile);
        session.getTransaction().commit();
    }
    
    public List findByExample(Mobile instance) {
         return session.createCriteria("pojo.Mobile")
                 .add(Example.create(instance))
                 .list();
    }

    public List getCompanyNames() {
        return session.createCriteria(Mobile.class)
                .setProjection(Projections.groupProperty("company"))
                .addOrder(Order.asc("company"))
                .list();
    }

    public void incrementQuantity(int parseInt) {
        Mobile mobile = getMobileById(parseInt);
        mobile.setQuantity(mobile.getQuantity() + 1);
        updateMobile(mobile);
    }
}

